package com.example.mobile2021606134chhnewdemo.MapModule;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.mobile2021606134chhnewdemo.ContactModule.ContactActivity;
import com.example.mobile2021606134chhnewdemo.NewsModule.NewsActivity;
import com.example.mobile2021606134chhnewdemo.R;
import com.example.mobile2021606134chhnewdemo.VideoModule.VideoActivity;
import com.example.mobile2021606134chhnewdemo.WeatherModule.WeatherActivity;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import me.sugarkawhi.bottomnavigationbar.BottomNavigationBar;
import me.sugarkawhi.bottomnavigationbar.BottomNavigationEntity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MapActivity extends AppCompatActivity {
    List<BottomNavigationEntity> entities = new ArrayList<>();
    private BottomNavigationBar bottomNavigationBar;
    private ImageView imgMapOrientation;
    private ImageView imgMapView;
    private Button btnMapZoomIn;
    private Button btnMapZoomOut;
    private BottomNavigationBar BNBMap;
    private String sip = "112.823257,23.001904";
    private int zoom = 15;
    private int traffic = 0;
    OkHttpClient okHttpClient = new OkHttpClient();
    private EditText edtMapAddress;
    private EditText edtMapCity;
    private Button btnMapOrientation;
    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_map);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
        initBottomBar();
        initView();

//      首次显示
        new Thread(new Runnable() {
            @Override
            public void run() {
                RequestBody requestBody = new FormBody.Builder()
                        .add("key", GaoDeMapDataManager.GAODEKEY)
                        .add("location", "112.823257,23.001904")
                        .add("zoom", "15")
                        .add("size", "680*460")
                        .build();
                Request request = new Request.Builder()
                        .url(GaoDeMapDataManager.pic)
                        .post(requestBody)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    //                    112.823257,23.001904
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String picUrl = GaoDeMapDataManager.pic + "location=" + sip
                                + "&zoom=" + zoom + "&size=" + "680*460" + "&traffic=" + traffic
                                + "&markers=mid,,A:" + sip + "&key=" + GaoDeMapDataManager.GAODEKEY;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Glide.with(getApplicationContext()).load(picUrl).into(imgMapView);
                            }
                        });
                    }
                });
            }
        }).start();

//        路况
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    traffic = 1;
                } else {
                    traffic = 0;
                }
                String picUrl = GaoDeMapDataManager.pic + "location=" + sip
                        + "&zoom=" + zoom + "&size=" + "680*460" + "&traffic=" + traffic
                        + "&markers=mid,,A:" + sip + "&key=" + GaoDeMapDataManager.GAODEKEY;
                Glide.with(getApplicationContext()).load(picUrl).into(imgMapView);
            }
        });

//        精确定位
        btnMapOrientation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody = new FormBody.Builder()
                                .add("key", GaoDeMapDataManager.GAODEKEY)
                                .add("address", edtMapAddress.getText().toString().trim())
                                .add("city", edtMapCity.getText().toString().trim())
                                .build();
                        Request request = new Request.Builder()
                                .url(GaoDeMapDataManager.geo)
                                .post(requestBody)
                                .build();
                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Gson gson = new Gson();
                                        GEO geo = gson.fromJson(result, GEO.class);
                                        List<GEO.GeocodesBean> lip = geo.getGeocodes();
                                        sip = lip.get(0).getLocation();
                                        String picUrl = GaoDeMapDataManager.pic + "location=" + sip
                                                + "&zoom=" + zoom + "&size=" + "680*460" + "&traffic=" + traffic + "&markers=mid,,A:" + sip
                                                + "&key=" + GaoDeMapDataManager.GAODEKEY;
                                        Glide.with(getApplicationContext()).load(picUrl).into(imgMapView);
                                    }
                                });
                            }
                        });
                    }
                }).start();
            }
        });

//        放大
        btnMapZoomIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (zoom < 17) {
                    zoom += 1;
                    String picUrl = GaoDeMapDataManager.pic + "location=" + sip
                            + "&zoom=" + zoom + "&size=" + "680*460" + "&traffic=" + traffic
                            + "&markers=mid,,A:" + sip + "&key=" + GaoDeMapDataManager.GAODEKEY;
                    Glide.with(getApplicationContext()).load(picUrl).into(imgMapView);
                } else {
                    Toast.makeText(getApplicationContext(), "以达极限！", Toast.LENGTH_SHORT).show();
                }
            }
        });

//        缩小
        btnMapZoomOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (zoom > 2) {
                    zoom -= 1;
                    String picUrl = GaoDeMapDataManager.pic + "location=" + sip
                            + "&zoom=" + zoom + "&size=" + "680*460" + "&traffic=" + traffic
                            + "&markers=mid,,A:" + sip + "&key=" + GaoDeMapDataManager.GAODEKEY;
                    Glide.with(getApplicationContext()).load(picUrl).into(imgMapView);
                } else {
                    Toast.makeText(getApplicationContext(), "以达极限！", Toast.LENGTH_SHORT).show();
                }
            }
        });

//        ip定位
        imgMapOrientation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody = new FormBody.Builder()
                                .add("key", GaoDeMapDataManager.GAODEKEY)
                                .build();
                        Request request = new Request.Builder()
                                .url(GaoDeMapDataManager.ipPositioning)
                                .post(requestBody)
                                .build();

                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Gson gson = new Gson();
                                        IP ip = gson.fromJson(result, IP.class);
                                        String mip = ip.getRectangle();
                                        sip = mip.split(";")[0];
//                                        112.823257,23.001904
                                        String picUrl = GaoDeMapDataManager.pic + "location=" + sip
                                                + "&zoom=" + zoom + "&size=" + "680*460" + "&traffic="
                                                + traffic + "&markers=mid,,A:" + sip
                                                + "&key=" + GaoDeMapDataManager.GAODEKEY;
                                        Glide.with(getApplicationContext()).load(picUrl).into(imgMapView);
                                    }
                                });
                            }
                        });
                    }
                }).start();
            }
        });

    }

    private void initBottomBar() {
        entities.add(new BottomNavigationEntity("通讯", R.drawable.communication_d, R.drawable.communication_s));
        entities.add(new BottomNavigationEntity("视频", R.drawable.play_d, R.drawable.play_s));
        entities.add(new BottomNavigationEntity("   ", R.drawable.new_d, R.drawable.new_s));
        entities.add(new BottomNavigationEntity("导航", R.drawable.navigation_d, R.drawable.navigation_s));
        entities.add(new BottomNavigationEntity("天气", R.drawable.weather_d, R.drawable.weather_s));
        bottomNavigationBar = findViewById(R.id.BNBMap);
        bottomNavigationBar.setEntities(entities);
        bottomNavigationBar.setCurrentPosition(3);

        bottomNavigationBar.setBnbItemDoubleClickListener(new BottomNavigationBar.IBnbItemDoubleClickListener() {
            @Override
            public void onBnbItemDoubleClick(int position) {
                Log.i("bottomNavigationBar", "重复选中" + position);
            }
        });

        bottomNavigationBar.setBnbItemSelectListener(new BottomNavigationBar.IBnbItemSelectListener() {
            @Override
            public void onBnbItemSelect(int position) {
                Log.i("bottomNavigationBar", "选中" + position);
                switch (position) {
                    case 0:
                        Intent intent3 = new Intent(getApplicationContext(), ContactActivity.class);
                        startActivity(intent3);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 1:
                        Intent intent4 = new Intent(getApplicationContext(), VideoActivity.class);
                        startActivity(intent4);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 2:
                        Intent intent = new Intent(getApplicationContext(), NewsActivity.class);
                        startActivity(intent);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                    case 3:
                        break;
                    case 4:
                        Intent intent1 = new Intent(getApplicationContext(), WeatherActivity.class);
                        startActivity(intent1);
                        finish();
                        overridePendingTransition(0, 0);
                        break;
                }
            }
        });
    }

    private void initView() {
        imgMapOrientation = (ImageView) findViewById(R.id.imgMapOrientation);
        imgMapView = (ImageView) findViewById(R.id.imgMapView);
        btnMapZoomIn = (Button) findViewById(R.id.btnMapZoomIn);
        btnMapZoomOut = (Button) findViewById(R.id.btnMapZoomOut);
        BNBMap = (BottomNavigationBar) findViewById(R.id.BNBMap);
        edtMapAddress = (EditText) findViewById(R.id.edtMapAddress);
        edtMapCity = (EditText) findViewById(R.id.edtMapCity);
        btnMapOrientation = (Button) findViewById(R.id.btnMapOrientation);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
    }
}